package com.sanploy.fed.push.util;

import com.sanploy.fed.push.bean.FileInfomation;
import com.sanploy.fed.push.dao.FileInfomationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/8/14.
 */
@Service
public class FileUploadUtil {
    private FileUploadUtil() {}
    private static final FileUploadUtil single = new FileUploadUtil();
    //静态工厂方法
    public static FileUploadUtil getInstance() {
        return single;
    }

    @Autowired
    private FileInfomationDao fileInfomationDao;

    private ExecutorService e = new ThreadPoolExecutor(3, 10, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100000),

            new ThreadPoolExecutor.CallerRunsPolicy());

    public void uploadVideo(final MultipartFile file){
        e.execute(new Runnable() {
            @Override
            public void run() {
                saveFile(file,"video");
            }
        });
    }

    public String uploadPhoto(final MultipartFile file){
                return saveFile(file, "photo");

    }

    private  String saveFile(MultipartFile file,String foldName) {

        FileInfomation fileInfomation = new FileInfomation();
        fileInfomation.setOpetime(DateUtil.getTimeNormal());
        String[] strings = file.getContentType().split("/");
        String uuid = UUID.randomUUID().toString();
        fileInfomation.setFilename(uuid + "."+strings[1]);
        fileInfomation.setFilepath(foldName+"/"+uuid + "."+strings[1]);
        fileInfomation.setFiletype(foldName);
        fileInfomation.setOldname(file.getOriginalFilename());
        String s = FileTest.getJarDir();

        if (s.startsWith("file")){
            s = FileTest.getJarDir().substring(6);
        }
        File file1 = new File(s,"resource"+File.separator+ foldName);
        if (!file1.exists()){
            System.out.println(file1.mkdirs());
        }
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
            String xx = file1.getAbsolutePath();
            if (xx.startsWith("file")){
                xx = FileTest.getJarDir().substring(6);
            }

            for (String x:strings){
                System.out.println(x);
            }

            BufferedOutputStream stream =
                    new BufferedOutputStream(new FileOutputStream(new File(xx,uuid + "."+strings[1])));
            stream.write(bytes);
            stream.close();
//            fileInfomationDao.save(fileInfomation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uuid + "."+strings[1];

    }
}
